package com.cloudkinto.controller.finance;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.finance.CostRecordService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@RestController
@Api(tags = "财务-支出记录模块")
@RequestMapping("/api/cost")
public class CostRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(CostRecordController.class);

    @Autowired
    private CostRecordService costRecordService;

    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", costRecordService.add(req, userId));
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", costRecordService.update(req, userId));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", costRecordService.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", costRecordService.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return costRecordService.pageInit();
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams(@ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）")
    )
    public SingleResult list(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", costRecordService.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    public PageResult listPage(String dateStart, String dateEnd, String type, String current, String pageSize, String keyword, String dateType) {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        map.put("dateStart", dateStart);
        map.put("dateEnd", dateEnd);
        map.put("current", current);
        if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        map.put("pageSize", pageSize);
        map.put("type", type);
        map.put("dateType", dateType);
        map.put("companyId", companyId + "");
        map.put("keyword", keyword);
        return costRecordService.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("exportExcel")
    @ApiOperation(value = "导出所有数据excel")
    public void exportAll(String dateStart, String dateEnd, String type, String current, String pageSize, String keyword,
                          String dateType, HttpServletResponse response) throws Exception {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        map.put("dateStart", dateStart);
        map.put("dateEnd", dateEnd);
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("type", type);
        map.put("dateType", dateType);
        if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        map.put("companyId", companyId + "");
        map.put("keyword", keyword);
        costRecordService.exportExcel(map, response);
    }

}

